﻿function EventKeyCode(e) {
    return window.event ? e.keyCode : e.which;
}
function CancelEventExecution(event) {
    if (navigator.appName == "Netscape") event.preventDefault();
    else event.returnValue = false;
}
function IsNumero(e) {
    return (EventKeyCode(e) >= 48 && EventKeyCode(e) <= 57) || (EventKeyCode(e) >= 96 && EventKeyCode(e) <= 95);
}
function IsKeyCodePermitido(e) {
    return (EventKeyCode(e) == 0 || EventKeyCode(e) == 8 || EventKeyCode(e) == 37 || EventKeyCode(e) == 39 || EventKeyCode(e) == 46 || EventKeyCode(e) == 36 || EventKeyCode(e) == 35);
}

function MascaraSoNumero(e) {
    if (!IsKeyCodePermitido(e))
        if (!IsNumero(e)) {
            CancelEventExecution(e);
            return false;
        } else
            return true;
    else return false;
}

function MascaraCpf(th, e) {
    if (MascaraSoNumero(e)) {
        th.value = th.value.replace(/^(\d{3})$/i, "$1.");
        th.value = th.value.replace(/^(\d{3}).(\d{3})$/i, "$1.$2.");
        th.value = th.value.replace(/^(\d{3}).(\d{3}).(\d{3})$/i, "$1.$2.$3-");
        cpf = th.value.replace(/[^\d]/gi, "");
        if (cpf.length == 11)
            th.value = cpf.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/i, "$1.$2.$3-$4");
    }
}
function MascaraCEP(th, e) {
    if (!IsKeyCodePermitido(e)) {
        if (th.value.length == 2) th.value = th.value + '.';
        if (th.value.length == 6) th.value = th.value + '-';
    }
}
function MascaraTelefone(th, e) {
    if (MascaraSoNumero(e)) {
        if (th.value.length == 1) th.value = '(' + th.value;
        if (th.value.length == 3) th.value = th.value + ')';
        if (th.value.length == 8) th.value = th.value + '-';
    }
}
function MascaraDecimalMonetario(th, e) {
    ret = th.value.replace(/[^\d\-]/gi, "").replace(/^[0]+/i, "");
    if (ret.length == 0 || ret == "") ret = '000';
    if (ret.length == 1) ret = '00' + ret;
    if (ret.length == 2) ret = '0' + ret;
    th.value = ret.replace(/(\d+)(\d{2})/i, "$1,$2");
}
function MascaraMesAno(th, e) {
    if (!IsKeyCodePermitido(e)) {
        if (th.value.length == 1 && th.value > 1) th.value = "0" + th.value;
        if (th.value.substr(0, 2) > 12) th.value = 1;
        th.value = th.value.replace(/[^\d]/gi, "").replace(/(\d{2})(\d{0,4})/i, "$1/$2");
    }
}
function MascaraCalendario(th, e) {
    if (!IsKeyCodePermitido(e)) {
        th.value = th.value.replace(/[^\d\/]/gi, "");
        if (th.value.length == 2) th.value = th.value + '/';
        if (th.value.length == 5) th.value = th.value + '/';
    }
}

function MascaraMesAno(th, e) {
    if (!IsKeyCodePermitido(e)) {
        th.value = th.value.replace(/[^\d\/]/gi, "");
        if (th.value.length == 2) th.value = th.value + "/";
    }
}

function MascaraSoNumeros(th, e) {
    if (!IsKeyCodePermitido(e)) {
        if (MascaraSoNumero(e))
            th.value = th.value.replace(/[^\d]/gi, "");
    }
}


function MaxLengthTextArea(th, Max) {
    if (th.value.length >= Max) {
        th.value = th.value.substr(0, Max);
        return false;
    }
}

function IsMesAno(th) {
    if (th.value.length == 7) {
        dt = th.value.split("/");
        if ((dt[0] > 12 || dt[0] < 1) || (dt[1] <= 1000 || dt[1] >= 2050))
            th.value = "";
    }
    else
        th.value = "";
}




/* ------------ Calendario --------------- */

function IsDate(th) {
    if ((th.value != 0 && th.value.length != 10) || (th.value.length > 0 && th.value.length == 10 && th.value.match(/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/i) == null)) {
        th.value = "";
            alert("Data inválida.");
        return false;
    }
}

function NSemana(dia, mes, ano) { return new Date(mes + "/" + dia + "/" + ano).getDay(); }
function QtdeDiaMes(mes, ano) { return new Date(ano, mes, 0).getDate(); }

function DefineData(thId, dia, mes, ano) {
    if (document.getElementById(thId + "cal") == null) {
        var div = document.createElement("div");
        div.id = thId + "cal";
        document.getElementById(thId).offsetParent.appendChild(div);
        // document.getElementById(thId).setAttribute("onblur", "IsDate(this);if(document.getElementById(\"" + thId + "tbl\") != null)if(document.getElementById(\"" + thId + "tbl\").getAttribute('s')==0)document.getElementById(\"" + thId + "\").offsetParent.removeChild(document.getElementById(\"" + thId + "cal\"));");
        //   document.getElementById(thId).setAttribute("onkeydown", document.getElementById(thId).getAttribute("onkeydown") + "if(document.getElementById(\"" + thId + "tbl\") != null)if(document.getElementById(\"" + thId + "tbl\").getAttribute('s')==0)document.getElementById(\"" + thId + "\").offsetParent.removeChild(document.getElementById(\"" + thId + "cal\"));");

        if (document.getElementById(thId).value != "") {
            dia = document.getElementById(thId).value.substr(0, 2);
            mes = document.getElementById(thId).value.substr(3, 2);
            ano = document.getElementById(thId).value.substr(6, 4);
        }

        div.innerHTML = Calendario(thId, parseInt(dia), parseInt(mes), ano);
    }
    else
        document.getElementById(thId + "cal").innerHTML = Calendario(thId, dia, mes, ano);
}

function SelecionaData(thId, dia, mes, ano) {
    if (mes < 10) mes = 0 + "" + mes;
    if (dia < 10) dia = 0 + "" + dia;
    document.getElementById(thId).setAttribute("s", 0);
    document.getElementById(thId).offsetParent.removeChild(document.getElementById(thId + "cal"));
    document.getElementById(thId).value = dia + "/" + mes + "/" + ano;
}

function fechar(thId, idTxt) {
    setTimeout(function () {
        if (document.getElementById(thId) != null)
            if (document.getElementById(thId).getAttribute("s") == 0) {
                try {
                    //document.getElementById(idTxt).offsetParent.removeChild(document.getElementById(idTxt + "cal"));
                } catch (err) {
                    return false;
                }
            }
    }, 1500);
}


function Calendario(thId, dia, mes, ano) {

    diaSelecionado = new Date();
    if (document.getElementById(thId).value != "") {
        diaSelecionado.setDate(document.getElementById(thId).value.substr(0, 2));
        diaSelecionado.setMonth(document.getElementById(thId).value.substr(3, 2) - 1);
        diaSelecionado.setYear(document.getElementById(thId).value.substr(6, 4));
    }

    var dt = new Date();
    QtdeSemanas = 1;
    mesAnt = mes == 1 ? 12 : mes - 1;
    mesAntAno = mes == 1 ? ano - 1 : ano;

    mesPro = mes == 12 ? 1 : mes + 1;
    mesProAno = mes == 12 ? ano + 1 : ano;

    mesExt = new Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro");

    write = "<div class='YCCalendarTable'><table s='0' id='" + thId + "tbl' onmouseout='this.setAttribute(\"s\", 0);fechar(this.id, \"" + thId + "\");' onmouseover='this.setAttribute(\"s\", 1)'><thead>";

    write += "<tr><td class='YCCalendarAnterior' onclick='javascript:DefineData(\"" + thId + "\"," + 0 + "," + mesAnt + "," + mesAntAno + ");void(0);'><div></div></td>";
    write += "<td colspan='5'>" + mesExt[mes - 1] + "/" + ano + "</td>";
    write += "<td class='YCCalendarProximo' onclick='javascript:DefineData(\"" + thId + "\", " + 0 + ", " + mesPro + ", " + mesProAno + ");void(0);'><div></div></td></tr>";

    write += "<tr><td>d</td><td>s</td><td>t</td><td>q</td><td>q</td><td>s</td><td>s</td></tr>";
    write += "</thead><tbody><tr>";

    for (i = QtdeDiaMes(mesAnt, mesAntAno) - ((NSemana(1, mes, ano) - 1)); i <= QtdeDiaMes(mesAnt, mesAntAno); i++)
        write += "<td class='YCTdDesativado'>" + i + "</td>";

    for (i = 1; i <= QtdeDiaMes(mes, ano); i++) {
        if (i == diaSelecionado.getDate() && mes == (diaSelecionado.getMonth() + 1) && ano == diaSelecionado.getFullYear())
            write += "<td class='YCCalendarDiaSelecionado' onclick='javascript:SelecionaData(\"" + thId + "\", " + i + ", " + mes + ", " + ano + ");void(0);'>" + i + "</td>";
        else
            write += "<td onclick='javascript:SelecionaData(\"" + thId + "\", " + i + ", " + mes + ", " + ano + ");void(0);'>" + i + "</td>";
        if (NSemana(i, mes, ano) == 6) {
            write += "</tr><tr>";
            QtdeSemanas = QtdeSemanas + 1;
        }
    }
    if (6 - NSemana(QtdeDiaMes(mes, ano), mes, ano) == 0)
        QtdeSemanas = QtdeSemanas - 1;

    diasProxMes = 0;
    for (i = 1; i <= 6 - NSemana(QtdeDiaMes(mes, ano), mes, ano); i++) {
        write += "<td class='YCTdDesativado'>" + i + "</td>";
        diasProxMes = i;
    }
    write += "</tr>";

    for (i = 0; i < (6 - QtdeSemanas); i++) {
        write += "<tr>";
        for (i2 = 0; i2 <= 6; i2++) {
            diasProxMes++;
            write += "<td class='YCTdDesativado'>" + diasProxMes + "</td>";
        }
        write += "</tr>";
    }

    write += "</tbody>";

    write += "<tfoot><tr><td colspan='7'><a href='javascript:SelecionaData(\"" + thId + "\", " + dt.getDate() + ", " + (dt.getMonth() + 1) + ", " + dt.getFullYear() + ");void(0);'>Hoje</a></td></tr></tfoot></table></div>";
    return write;
}